package trss.webapp.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import trss.model.Torrent;
import trss.model.User;
import trss.service.TorrentManager;
import trss.service.UserManager;

@Controller
@RequestMapping("/torrent/**")
public class TorrentDownloadController {
    private static final Logger logger = LoggerFactory.getLogger(TorrentDownloadController.class);
    TorrentManager torrentManager;
    /**
     * @param torrentManager the torrentManager to set
     */
    @Autowired
    public void setTorrentManager(TorrentManager torrentManager) {
        this.torrentManager = torrentManager;
    }
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
    @RequestMapping(value = "/torrent/{torrentId}/{fileName}", method = RequestMethod.GET)
    public String list(@PathVariable Long torrentId, @PathVariable String fileName, ModelMap modelMap) {
        Torrent torrent = torrentManager.get(torrentId);
        modelMap.addAttribute("torrent", torrent);
        return "torrentDownloadView";
    }
}
